﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
    public partial class FormAdapter : FormBase
    {
        public FormAdapter()
        {
            InitializeComponent();

        }

        public FormAdapter(ToolStripMenuItem tsMi)
            : base(tsMi)
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
        }

        private void FormAdapter_Load(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master";
            //conn.StateChange += new StateChangeEventHandler(conn_StateChange);
            //conn.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from menu;select * from tpStu;select * from useraccount";

            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = cmd;

            DataSet ds = new DataSet();
            //DataTable dt = new DataTable();
            //1）如果连接对象已经打开，adapter使用连接对象，并不改变连接对象状态 
            //2）如果连接对象关闭，adapter打开连接对象，执行数据填充，然后关闭连接
            adapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView2.DataSource = ds.Tables[0];

            BindingSource bs = new BindingSource();
            bs.DataSource = ds.Tables[1];
            dataGridView3.DataSource = bs;

            bs = new BindingSource();
            bs.DataSource = ds.Tables[2];
            dataGridView4.DataSource = bs;
            bindingNavigator1.BindingSource = bs;
        }

        void conn_StateChange(object sender, StateChangeEventArgs e)
        {
            MessageBox.Show("ok");
        }
    }
}
